package com.ysstech.finance.controller;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.ysstech.common.annotation.YssLog;
import com.ysstech.common.annotation.YssPermissions;
import com.ysstech.common.controller.BaseController;
import com.ysstech.common.entity.Dicvalue;
import com.ysstech.common.entity.TreeNode;
import com.ysstech.common.exception.BusinessException;
import com.ysstech.common.util.resultutil.RestResult;
import com.ysstech.common.util.resultutil.ResultGenerator;
import com.ysstech.finance.entity.Account;
import com.ysstech.finance.entity.Voucher;
import com.ysstech.finance.enums.FinanceEnum;
import com.ysstech.finance.service.FinAccountService;
import com.ysstech.finance.service.VoucherService;
import com.ysstech.finance.thread.VoucherMainGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lishuangliang
 * @since 2021-03-10
 */
@Controller
@Slf4j
@Api(tags = "凭证管理api")
@RequestMapping("/voucher")
public class VoucherController extends BaseController {

    @Autowired
    private VoucherService voucherService;
    @Autowired
    private FinAccountService finAccountService;
    @Autowired
    private VoucherMainGenerator voucherMainGenerator;

    @YssPermissions(value = {"finance:voucher:voucher"})
    @ApiOperation(value = "跳转凭证管理页面")
    @YssLog(module = "凭证管理", remark = "跳转凭证管理页面")
    @RequestMapping(value = "/page")
    public String page() throws Exception {
        return "finance/voucher";
    }

    @YssPermissions(value = {"finance:voucher:voucher"})
    @ApiOperation(value = "跳转凭证管理页面")
    @YssLog(module = "凭证管理", remark = "跳转凭证管理页面")
    @RequestMapping(value = "/page2")
    public String produceVoucher() throws Exception {
        return "finance/produceVoucher";
    }


    @YssPermissions(value = {"finance:voucher:voucher"})
    @ApiOperation(value = "获取凭证页面初始值")
    @YssLog(module = "凭证管理", remark = "获取凭证页面初始值")
    @PostMapping(value = "/init")
    @ResponseBody
    public RestResult init() throws Exception {
        Map<String, Object> map = new HashMap<>();
        Account account = new Account();
        List<TreeNode> lsetlists = finAccountService.queryAccountTree(account);
        map.put("lsetlists", lsetlists);
        // 获取所有凭证来源
        List<Dicvalue> voucherSourceList = dicValueService.getCacheDicValueList(FinanceEnum.VOUCHER_SOURCE.getCode());
        map.put("voucherSourceList", voucherSourceList);
        map.put("voucherSourceList2", voucherSourceList);
        return ResultGenerator.getSuccessResult("获取页面初始化信息成功", map);
    }


    /**
     * 获取凭证信息
     *
     * @return
     */
    @YssPermissions(value = {"finance:voucher:voucher"})
    @ApiOperation(value = "获取凭证信息")
    @YssLog(module = "凭证管理", remark = "获取凭证信息")
    @PostMapping(value = "/listInfo")
    @ResponseBody
    public RestResult listInfo(Voucher voucher, String page, String rows) throws Exception {
        return ResultGenerator.getSuccessResult("获取凭证信息成功",
                voucherService.queryAllVoucher(voucher, this.getPage(page, rows)));
    }

    /**
     * 获取一张凭证信息
     *
     * @return
     */
    @YssPermissions(value = {"finance:voucher:voucher"})
    @ApiOperation(value = "获取一张凭证信息")
    @YssLog(module = "凭证管理", remark = "获取一张凭证信息")
    @PostMapping(value = "/listInfoByNum")
    @ResponseBody
    public RestResult listInfoByNum(Voucher voucher) throws Exception {
        if (null == voucher || StringUtils.isBlank(voucher.getNum())) {
            throw new BusinessException("获取凭证信息失败，请稍后重试！");
        }
        return ResultGenerator.getSuccessResult("获取一张凭证信息成功",
                voucherService.queryAllVoucher(voucher));
    }


    /**
     * 生成取凭证信息
     *
     * @return
     */
    @YssPermissions(value = {"finance:voucher:voucher"})
    @ApiOperation(value = "生成凭证")
    @YssLog(module = "凭证管理", remark = "生成凭证")
    @PostMapping(value = "/produceVoucher")
    @ResponseBody
    public RestResult produceVoucher(Voucher voucher) throws Exception {
        if (null == voucher || StringUtils.isBlank(voucher.getAccountId())) {
            throw new BusinessException("获取凭证信息失败，请稍后重试！");
        }
        List<String> list = JSONArray.parseArray(voucher.getAccountId(), String.class);
        if (CollectionUtils.isEmpty(list)) {
            throw new BusinessException("获取凭证信息失败，请稍后重试！");
        }
        voucherMainGenerator.renewVoucher(voucher, list);
        return ResultGenerator.getSuccessResult("获取凭证信息成功");
    }

}
